<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Git Push 避免用户名和密码方法</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="Zendwind">

    <!-- Le styles -->
    <link rel="stylesheet" href="../../../../theme/css/bootstrap.min.css" type="text/css" />
    <style type="text/css">
      body {
        padding-top: 60px;
        padding-bottom: 40px;
      }
      .sidebar-nav {
        padding: 9px 0;
      }
      .tag-1 {
        font-size: 13pt;
      }
      .tag-2 {
        font-size: 10pt;
      }
      .tag-2 {
        font-size: 8pt;
      }
      .tag-4 {
        font-size: 6pt;
     }
    </style>
    <link href="../../../../theme/css/bootstrap-responsive.min.css" rel="stylesheet">
        <link href="../../../../theme/css/font-awesome.css" rel="stylesheet">

    <link href="../../../../theme/css/pygments.css" rel="stylesheet">

    <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="../../../../theme/images/favicon.ico">
    <link rel="apple-touch-icon" href="../../../../theme/images/apple-touch-icon.png">
    <link rel="apple-touch-icon" sizes="72x72" href="../../../../theme/images/apple-touch-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="114x114" href="../../../../theme/images/apple-touch-icon-114x114.png">

    <link href="../../../../" type="application/atom+xml" rel="alternate" title="ZendWind ATOM Feed" />
    <link href="../../../../feeds/all.rss.xml" type="application/atom+xml" rel="alternate" title="ZendWind RSS Feed" />

  </head>

  <body>

    <div class="navbar navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container-fluid">
          <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </a>
          <a class="brand" href="../../../../index.html">ZendWind </a>
          <div class="nav-collapse">
            <ul class="nav">
                  <li><a href="http://www.zendwind.com"><font color="#58ACFA">主页</font></a></li>
                  <li><a href="http://www.zendwind.com/category/c.html"><font color="#58ACFA">C/C++</font></a></li>
                  <li><a href="http://www.zendwind.com"><font color="#58ACFA">Linux</font></a></li>
                  <li><a href="http://www.zendwind.com/category/python.html"><font color="#58ACFA">Python</font></a></li>
                  <li><a href="http://www.zendwind.com/feeds/all.atom.xml"><font color="#58ACFA">Feed</font></a></li>
                  <li><a href="http://www.zendwind.com"><font color="#58ACFA">生活杂谈</font></a></li>
                  <li><a href="http://www.zendwind.com/category/about.html"><font color="#58ACFA">About</font></a></li>
                          <li class="divider-vertical"></li>
		    </ul>
            <!--<p class="navbar-text pull-right">Logged in as <a href="#">username</a></p>-->
          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>

    <div class="container-fluid">
      <div class="row">
        <div class="span9" id="content">
<section id="content">
        <article>
                <header>
                        <h1>
                                <a href=""
                                        rel="bookmark"
                                        title="Permalink to Git Push 避免用户名和密码方法">
                                        Git Push 避免用户名和密码方法
                                </a>
                        </h1>
                </header>
                <div class="entry-content">
                <div class="well">
<footer class="post-info">
<span class="label">Date</span>
<abbr class="published" title="2013-11-27T00:00:00">
        <i class="icon-calendar"></i>周三 27 十一月 2013
</abbr>
<span class="label">By</span>
<a href="../../../../author/zendwind.html"><i class="icon-user"></i>Zendwind</a>
<!--
<span class="label">Category</span>
<a href="../../../../category/git.html"><i class="icon-folder-open"></i>GIT</a>.
-->

<span class="label">Tags</span>
	<a href="../../../../tag/git.html"><i class="icon-tag"></i>git</a>
	<a href="../../../../tag/push.html"><i class="icon-tag"></i>push</a>
</footer><!-- /.post-info -->                </div>
                <h2>Git Push 避免用户名和密码方法</h2>
<p>在Git push过程中每次都要输入用户名和密码，真是恼火，严重降低了效率，本文从网上找了两中方法进行尝试，都可以避免这种情况，接下来将依次介绍在windows环境中的应用（Linux环境类似）。
<h4><font color="blue">1.方法一</font></h4></p>
<p><strong>1.1 创建文件存储GIT用户名和密码</strong></p>
<p>在%HOME%目录中，一般为C:\users\Administrator，也可以是你自己创建的系统用户名目录，反正都在C:\users\中。文件名为.git-credentials,由于在Window中不允许直接创建以"."开头的文件，所以需要借助git bash进行，打开git bash客户端，进行%HOME%目录，然后用touch创建文件 .git-credentials, 用vim编辑此文件，输入内容格式：</p>
<p><code>touch .git-credentials</code></p>
<p><code>vim .git-credentials</code></p>
<p><code>https://{username}:{password}@github.com</code></p>
<p><strong>1.2 添加Git Config 内容</strong></p>
<p>进入git bash终端， 输入如下命令：</p>
<p><code>git config --global credential.helper store</code></p>
<p>执行完后查看%HOME%目录下的.gitconfig文件，会多了一项：</p>
<div class="highlight"><pre><span class="k">[credential]</span>

    <span class="na">helper</span> <span class="o">=</span> <span class="s">store</span>
</pre></div>


<p>重新开启git bash会发现git push时不用再输入用户名和密码</p>
<h4><font color="blue">2.方法二</font></h4>

<p><strong>2.1 添加环境变量</strong></p>
<p>在windows中添加一个HOME环境变量，变量名:HOME,变量值：%USERPROFILE%</p>
<p><img alt="" src="http://i.imgur.com/dG08KLU.jpg" /></p>
<p><strong>2.2 创建git用户名和密码存储文件</strong></p>
<p>进入%HOME%目录，新建一个名为"_netrc"的文件，文件中内容格式如下：</p>
<div class="highlight"><pre><span class="n">machine</span> <span class="p">{</span><span class="n">git</span> <span class="n">account</span> <span class="n">name</span><span class="p">}.</span><span class="n">github</span><span class="p">.</span><span class="n">com</span>
<span class="n">login</span> <span class="n">your</span><span class="o">-</span><span class="n">usernmae</span>
<span class="n">password</span> <span class="n">your</span><span class="o">-</span><span class="n">password</span>
</pre></div>


<p>重新打开git bash即可，无需再输入用户名和密码</p>
                </div><!-- /.entry-content -->
        </article>
</section>
        </div><!--/span-->
		
        <div class="span3 well sidebar-nav" id="sidebar">
<ul class="nav nav-list">
<li class="nav-header"><h4>分类目录</h4></li>
<hr color=#987cb9 SIZE=3 />
<li>
	<a href="../../../../category/about.html">
		About
	</a>	
</li>
<li>
	<a href="../../../../category/git.html">
		GIT
	</a>	
</li>
<li>
	<a href="../../../../category/python.html">
		Python
	</a>	
</li>

<li class="nav-header"><h4>标签云</h4></li>
<hr color=#987cb9 SIZE=3 />
<a href="../../../../tag/pygame.html">
        pygame&nbsp;&nbsp;
</a>
<a href="../../../../tag/eclipse.html">
        eclipse&nbsp;&nbsp;
</a>
<a href="../../../../tag/pelican.html">
        pelican&nbsp;&nbsp;
</a>
<a href="../../../../tag/django.html">
        Django&nbsp;&nbsp;
</a>
<a href="../../../../tag/push.html">
        push&nbsp;&nbsp;
</a>
<a href="../../../../tag/about.html">
        about&nbsp;&nbsp;
</a>
<a href="../../../../tag/git.html">
        git&nbsp;&nbsp;
</a>
<br />
<br />
<li class="nav-header"><h4>友情链接</h4></li>
<hr color=#987cb9 SIZE=3 />
    <li><a href="http://bbs.chinaunix.net/forum.php?gid=277" target=_blank>ChinaUnix运维专栏</a></li>
    <li><a href="http://bbs.linuxtone.org/forum.php" target=_blank>LinuxTone运维专栏</a></li>
    <li><a href="http://wiki.saltstack.cn/" target=_blank>SaltStack维基-自动化运维</a></li>
    <li><a href="http://www.huxiu.com/" target=_blank>虎嗅网-商业资讯与交流平台</a></li>
    <li><a href="http://www.36kr.com/" target=_blank>36氪-互联网创业</a></li>


</ul>        </div><!--/.well -->
		<!--add by zendwind-->
		<div class="span9" id="content">
			<hr size=2>
<!-- Baidu Button BEGIN -->
<div id="bdshare" class="bdshare_t bds_tools get-codes-bdshare">

<a class="bds_qzone"></a>
<a class="bds_tsina"></a>
<a class="bds_tqq"></a>
<a class="bds_renren"></a>
<a class="bds_t163"></a>
<span class="bds_more"></span>
</div>
<script type="text/javascript" id="bdshare_js" data="type=tools&amp;uid=0" ></script>
<script type="text/javascript" id="bdshell_js"></script>
<script type="text/javascript">
document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000)
</script>
<!-- Baidu Button END -->		</div>
		
		<div class="span9" id="content">
			<hr size=2>

<!-- Duoshuo Comment BEGIN -->
<div class="ds-thread"></div>
<script type="text/javascript">
var duoshuoQuery = {short_name:"zendwind"};
	(function() {
		var ds = document.createElement('script');
		ds.type = 'text/javascript';ds.async = true;
		ds.src = 'http://static.duoshuo.com/embed.js';
		ds.charset = 'UTF-8';
		(document.getElementsByTagName('head')[0] 
		|| document.getElementsByTagName('body')[0]).appendChild(ds);
	})();
	</script>
<!-- Duoshuo Comment END -->		</div>
		 
      </div><!--/row-->
	   
      <hr>

      <footer>
        <address id="about">
        <<center>Power by Pelican, Copyright@ <a href="http://www.zendwind.com">ZendWind</a></center>
        </address><!-- /#about -->
		<p><center> @ZendWind Personal Blog </center></p>
      </footer>

    </div><!--/.fluid-container-->


<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
	var pageTracker = _gat._getTracker("UA-46020847-1");
pageTracker._trackPageview();
} catch(err) {}</script>
	

    <!-- Le javascript -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="../../../../theme/js/jquery-1.7.2.min.js"></script>
    <script src="../../../../theme/js/bootstrap.min.js"></script>
  </body>
</html>